<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        class Set {
            items = {}

            add(value) {
                if (this.has(value)) {
                    return fasle
                }
                this.items[value] = value
                return true
            }

            has(value) {
                return Object.prototype.hasOwnProperty.call(this.items, value)
            }

            remove(value){
                if (this.has(value)) {
                    return false
                }
                delete this.items[value]
                return true
            }
            clear() {
                this.items = {}
                return true
            }
            size() {
                return Object.keys(this.items).length
            }
            values() {
                return Object.keys(this.items)
            }

            // 并集
            union(otherSet) {
                let unionSet = new Set()
                let ovalues = this.values()
                for(let i=0; i<this.size(); i++) {
                    unionSet.add(ovalues[i])
                }
                console.log(unionSet)

                let values = otherSet.values()
                for(let i=0; i<otherSet.size(); i++) {
                    unionSet.add(values[i])
                }
                return unionSet
            }

            // 交集
            intersection(otherSet) {
                let values = this.values()
                console.log(values)
                console.log(otherSet)
                let intersectionSet = new Set()
                for(let i=0; i<this.size(); i++) {
                    if(otherSet.has(values[i])) {
                        intersectionSet.add(values[i])
                    }
                }
                return intersectionSet
            }

            // 差集
            difference(otherSet) {
               let values =  this.values()
               let difference = new Set()
               for(let i=0; i<this.size(); i++) {
                   if(!otherSet.has(values[i])) {
                       difference.add(values[i])
                   }
               }
               return difference
            }

            // 子集
            subset(otherSet) {
                let values = this.values()
                let subset = new Set()
                let otherValues = otherSet.values()
                for(let i=0; i<otherSet.size(); i++) {
                    if (!this.has(otherValues[i])) {
                        return false
                    }
                }
                return true
            }
        }

        let set = new Set()
        // console.log(set.has('aaa'))
        set.add('bbb')
        set.add('aaa')
        // console.log(set)
        // console.log(set)
        // console.log(set.size())
        // console.log(set.values())
        // console.log('remove',set.remove('aaa'))
        // console.log(set.values())
        // console.log(set.clear())

        // let oSet = new Set()
        // oSet.add('aaa')
        // console.log(set.union(oSet))
        
        let oSet = new Set()
        oSet.add('aaa')
        oSet.add('ccc')
        // console.log(set.intersection(oSet))
        console.log(set.difference(oSet))

        console.log(set.subset(oSet))
    </script>
</body>
</html>